// pages/index/index.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    openid:"",
    ent_id:"2",
    kefu_name:"kefu2",
    nickname:"",
    tel:"",
    avatar:"",
    score:"",
    company_name:"",
    company_pic:"",
    acount_openid:"",
  },
  //咨询店铺客服
  callKefu(){
     var url=app.globalData.apiUrl+"/chatIndex?kefu_id="+this.data.kefu_name+"&ent_id="+this.data.ent_id+"&visitor_name="+this.data.nickname+"&avator="+this.data.avatar;
     wx.navigateTo({
      url: "/pages/index/kefu?url="+encodeURIComponent(url)
    })
  },
  //按钮回调的方法
  getPerson:function(e){
    console.log(e);
    var username=e.detail.userInfo.nickName;
    var  avator=e.detail.userInfo.avatarUrl;
    wx.navigateTo({
      url: '/pages/member/index?nickname='+username+'&avatar='
      +avator+'&openid='
      +this.data.openid
      +"&ent_id="+this.data.ent_id
      +"&kefu_name="+this.data.kefu_name
    })

  },
  //获取会员信息
  getCustomer(openid,entId){
    var self=this;
    //通过code调用自己服务接口获取到openid
    wx.request({
      url: app.globalData.apiUrl+'/2/getCustomerInfo',
      data: {
        openid: openid,
        ent_id:entId
      },
      success:function(wxInfo){
        console.log(wxInfo);
        var res=wxInfo.data.result;
        if(!res){
          return;
        }
        var company_name="";
        if(res.kefu_nickname){
          company_name=res.kefu_nickname;
        }
        var company_pic="/pages/static/images/shop.png";
        if(res.company_pic){
          company_pic=res.company_pic;
        }

        self.setData({
          openid:res.open_id,
          nickname:res.nickname,
          avatar:res.avatar,
          score:res.score,
          tel:res.tel,
          company_name:company_name,
          company_pic:company_pic
        });
      }
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var self=this;
    if(options.acount_openid){
      wx.setStorage({
        key: 'acount_openid',
        data: options.acount_openid
        })
        this.setData({
          acount_openid:options.acount_openid
        });
    }
    if(options.ent_id && options.kefu_name){
      this.setData({
        ent_id:options.ent_id,
        kefu_name:options.kefu_name
      });
    }
    wx.login({
      success(res) {
        //js调用登陆命令获取到code
        if (!res.code) {
            console.log('登录失败！' + res.errMsg)
            return;
        }
        wx.request({
          url: app.globalData.apiUrl+'/wechat/miniUserInfo',
          data: {
            code: res.code,
            entId:self.data.ent_id
          },
          success:function(wxInfo){
            console.log(wxInfo);
            var res=wxInfo.data.result;
            if(!res){
              return;
            }
            var company_name="";
            if(res.kefu_nickname){
              company_name=res.kefu_nickname;
            }
            var company_pic="/pages/static/images/shop.png";
            if(res.company_pic){
              company_pic=res.company_pic;
            }
            if(res.open_id){
              wx.setStorage({
                key: 'openid',
                data: res.open_id
                })
            }
            self.setData({
              openid:res.open_id,
              nickname:res.nickname,
              avatar:res.avatar,
              score:res.score,
              tel:res.tel,
              company_name:company_name,
              company_pic:company_pic
            });
          }
        });
      }
    })

 
  },
//获取用户信息
getUserProfile(e) {
  // 推荐使用 wx.getUserProfile 获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认
  // 开发者妥善保管用户快速填写的头像昵称，避免重复弹窗
  wx.getUserProfile({
    desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
    success: (res) => {
      var userInfo=res.userInfo;
      var username=userInfo.nickName;
      var  avator=userInfo.avatarUrl;
      wx.navigateTo({
        url: '/pages/member/index?nickname='+username+'&avatar='
        +avator+'&openid='
        +this.data.openid
        +"&ent_id="+this.data.ent_id
        +"&kefu_name="+this.data.kefu_name
        +"&acount_openid="+this.data.acount_openid
      })
    }
  })
},
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    let _this = this
    wx.getStorage({
        key: 'openid',
        success(res) {
          console.log(res.data)
          _this.getCustomer(res.data,_this.data.ent_id);
        }
    });
    wx.getStorage({
      key: 'acount_openid',
      success(res) {
        _this.setData({
          acount_openid:res.data
        });
      }
  });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})